﻿/**
* 该类为滚动主程序类
* @author lbynet
* @version 0.1
*/

package com.xuanque.mahjongDice.ScrollBarui{
	import flash.display.*;
	import flash.events.*;

	public class ScrollBarX extends MovieClip {
		
		private var xOffset:Number;
		private var xMin:Number = 0;
		private var xMax:Number;
		private var speed:Number = 10;
		private var newwidth:Number = 0;
		
		public function ScrollBarX():void {
			
			//xMax = track.width - thumb.width;
			//init();
		}
		
		public function init() {
			
			this.newWidth();
			
			thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, thumbUp);

			left_btn.addEventListener(MouseEvent.MOUSE_DOWN, leftHandler);
			right_btn.addEventListener(MouseEvent.MOUSE_DOWN, rightHandler);
			track.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler);
		}
		
		
		private function thumbDown(e:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_MOVE,thumbMove);
			xOffset=mouseX - thumb.x;
		}

		private function thumbUp(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,thumbMove);
		}

		private function thumbMove(e:MouseEvent):void {
			thumb.x = mouseX - xOffset;
			if (thumb.x <= xMin) {
				thumb.x=xMin;
			}
			if (thumb.x >= xMax) {
				thumb.x=xMax;
			}
			dispatch(e);
		}
		private function leftHandler(e:MouseEvent):void {

			thumb.x -= this.speed;

			if (thumb.x <= xMin) {
				thumb.x=xMin;
			}
			dispatch(e);
		}
		private function rightHandler(e:MouseEvent):void {

			thumb.x += this.speed;

			if (thumb.x >= xMax) {
				thumb.x=xMax;
			}
			dispatch(e);
		}
		private function clickHandler(e:MouseEvent):void {
			var _mc:MovieClip = e.target as MovieClip;
			if (_mc.mouseX > xMax) {
				thumb.x = xMax;
			} else {
				thumb.x = _mc.mouseX;
			}
			//trace("_mc.mouseX :" + _mc.mouseX);
			//trace("xMax :" + xMax);
			dispatch(e);
		}
		
		private function dispatch(e:MouseEvent) {
			
			dispatchEvent(new ScrollBarXEvent(thumb.x / xMax));
			e.updateAfterEvent();
		}
		
		public function newWidth(num:Number = 0):void {
			
			if (num == 0) {
				this.newwidth = track.width - thumb.width;
			}else {
				thumb.width = num;
				this.newwidth = track.width - num;
			}
			xMax = this.newwidth
		}
		
		private function refresh() {
			
		}
	}
}